package com.jy.service.filemanager.user;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jy.entity.filemanager.TSUser;
import com.jy.repository.filemanager.TuserDao;
import com.jy.service.base.BaseServiceImp;
import com.jy.task.utils.TaskOperLogUtil;

@Service("tUserService")
public class TuserServiceImp extends BaseServiceImp<TSUser> implements
		TuserService {

	/** 调度工厂Bean */
	@Autowired
	private Scheduler scheduler;

	@Override
	@Transactional
	public int creatUser(TSUser o) {
		int res = 0;
		try {
			o.setCreateDate(new Date());
			// 更新数据库
			super.insert(o);
			TaskOperLogUtil.saveTaskLog(1, "用户", "新增用户信息");
			res = 1;
		} catch (Exception e) {
			logger.error("创建任务失败", e);
		}
		return res;
	}

	@Override
	@Transactional
	public int updateUser(TSUser o) {
		int res = 0;
		try {
			TuserDao dao = (TuserDao) baseDao;
			// 从数据库查找原信息
			dao.update(o);
			TaskOperLogUtil.saveTaskLog(2, "用户", "修改用户信息");
			res = 1;
		} catch (Exception e) {
			logger.error("创建任务失败", e);
		}
		return res;
	}

	@Override
	@Transactional
	public int deleteUser(TSUser o) {
		int res = 0;
		try {
			TuserDao dao = (TuserDao) baseDao;
			// 从数据库查找原信息
			dao.deleteUser(o);
			TaskOperLogUtil.saveTaskLog(3, "用户", "删除用户信息");
			res = 1;
		} catch (Exception e) {
			logger.error("删除任务失败", e);
		}
		return res;
	}

	@Override
	@Transactional
	public TSUser findUserById(String id) {
		TSUser obj = new TSUser();
		TuserDao dao = (TuserDao) baseDao;
		// 从数据库查找原信息
		obj = dao.findUserByPId(id);

		return obj;
	}

	@Override
	@Transactional
	public List<TSUser> getUserListByName(String name) {
		List<TSUser> objList = new ArrayList<TSUser>();
		try {
			TuserDao dao = (TuserDao) baseDao;
			// 从数据库查找原信息
			objList = dao.getTuserListByName(name);
		} catch (Exception e) {
			logger.error("获取数据失败失败", e);
		}
		return objList;
	}

	@Override
	public List<TSUser> getPrincipal() {
		TSUser obj = new TSUser();
		List<TSUser> objNew = null;
		try {
			TuserDao dao = (TuserDao) baseDao;
			// 从数据库查找原信息
			objNew = dao.getPrincipal(obj);
		} catch (Exception e) {
			logger.error("获取任务失败失败", e);
		}
		return objNew;
	}

}
